package com.linkoog.devtools.archive.util;

import com.intellij.ide.projectView.ProjectViewNode;
import com.intellij.ide.projectView.impl.nodes.PsiDirectoryNode;
import com.intellij.ide.projectView.impl.nodes.PsiFileNode;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class SelectedItemsUtil {


    public static List<VirtualFile> getSelectedItems(@NotNull AnActionEvent event) {
        Object[] selectedItems = event.getData(PlatformDataKeys.SELECTED_ITEMS);
        if (selectedItems == null || selectedItems.length == 0){
            return new ArrayList<>();
        }

        List<VirtualFile> selectedFiles = new ArrayList<>();
        for (Object selectedItem : selectedItems) {
            if (selectedItem instanceof ProjectViewNode<?> selectedItemNode){
                VirtualFile virtualFile = selectedItemNode.getVirtualFile();
                if (virtualFile != null && virtualFile.isInLocalFileSystem()) {
                    selectedFiles.add(virtualFile);
                }
            }
        }
        return selectedFiles;
    }

}
